package com.lechuang.system.manager.impl;

import cn.hutool.core.bean.BeanUtil;
import com.lechuang.system.dto.HomeImgSaveDTO;
import com.lechuang.system.entity.HomeImg;
import com.lechuang.system.manager.HomeImgManager;
import com.lechuang.system.service.HomeImgService;
import com.lechuang.system.vo.HomeImgDetailVO;
import com.lechuang.system.vo.HomeImgListVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HomeImgManagerImpl implements HomeImgManager {
    @Resource
    private HomeImgService homeImgService;

    @Override
    public List<HomeImgListVO> list() {
        List<HomeImg> list = homeImgService.list();
        return BeanUtil.copyToList(list, HomeImgListVO.class);
    }

    @Override
    public void delete(Long id) {
        homeImgService.removeById(id);
    }

    @Override
    public HomeImgDetailVO detail(Long id) {
        HomeImg homeImg = homeImgService.getById(id);
        return BeanUtil.copyProperties(homeImg, HomeImgDetailVO.class);
    }

    @Override
    public void saveOrUpdate(HomeImgSaveDTO homeImgSaveDTO) {
        Long id = homeImgSaveDTO.getId();
        if(id == null){
            this.save(homeImgSaveDTO);
        }else{
            this.update(homeImgSaveDTO);
        }
    }

    private void save(HomeImgSaveDTO homeImgSaveDTO) {
        HomeImg homeImg = BeanUtil.copyProperties(homeImgSaveDTO, HomeImg.class);
        homeImgService.save(homeImg);
    }

    private void update(HomeImgSaveDTO homeImgSaveDTO) {
        HomeImg homeImg = BeanUtil.copyProperties(homeImgSaveDTO, HomeImg.class);
        homeImg.setId(homeImgSaveDTO.getId());
        homeImgService.updateById(homeImg);
    }
}
